package cn.dnui_cr503.dnuifood_16110100503.controller;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import cn.dnui_cr503.dnuifood_16110100503.R;
import cn.dnui_cr503.dnuifood_16110100503.bean.Mebean;
import cn.dnui_cr503.dnuifood_16110100503.listener.OnRetrofitListener;
import cn.dnui_cr503.dnuifood_16110100503.model.MeModel;

public class AlterUserInfoActivity extends Activity implements OnRetrofitListener<Mebean> {
    private EditText alter_name,alter_pass,alter_num,alter_adress;
    private Button alter_button;
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alteruserinfo);

        initViews();
        initDate();
        initEvents();
    }

    private void initViews() {
        alter_name=findViewById(R.id.alter_name);
        alter_pass=findViewById(R.id.alter_pass);
        alter_num=findViewById(R.id.alter_num);
        alter_adress=findViewById(R.id.alter_adress);

        alter_button=findViewById(R.id.alter_button);
    }

    private void initEvents() {
        alter_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alter();
            }
        });
    }

    private void alter() {
        sharedPreferences=getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        String user_id = sharedPreferences.getString("user_id","user_id");

        MeModel meModel=new MeModel();
        meModel.updateUserById(user_id,alter_name.getText().toString(),alter_pass.getText().toString(),alter_num.getText().toString(),alter_adress.getText().toString(),this);
    }

    private void initDate() {
        sharedPreferences=getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        String user_id = sharedPreferences.getString("user_id","user_id");

        MeModel meModel=new MeModel();
        meModel.getUserById(user_id,this);
    }

    @Override
    public void onSuccess(Mebean object, int flag) {
        switch (flag){
            case 1:
                alter_name.setText(object.getUsername());
                alter_pass.setText(object.getUserpass());
                alter_num.setText(object.getMobilenum());
                alter_adress.setText(object.getAddress());
                break;
            case 2:
                if(object.getSuccess().equals("1")){
                    Toast.makeText(AlterUserInfoActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(AlterUserInfoActivity.this,"修改失败",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    @Override
    public void onFailure(String msg) {
        Toast.makeText(AlterUserInfoActivity.this,"联网失败",Toast.LENGTH_SHORT).show();
    }
}
